// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "CommonFuntionBPLibrary.generated.h"

UCLASS()
class UCommonFuntionBPLibrary : public UBlueprintFunctionLibrary
{
	GENERATED_UCLASS_BODY()

	UFUNCTION(BlueprintCallable,CustomThunk,meta=(CustomStructureParam="Struct"))
	static bool JsonStringToStruct(const FString& JsonString, int32& OutputStruct);
	static bool Generic_JsonStringToStruct(const FString& JsonString,UScriptStruct* OutputType, void* StructPtr);
	DECLARE_FUNCTION(execJsonStringToStruct)
	{
		P_GET_PROPERTY(FStrProperty, JsonString);
		
		Stack.StepCompiledIn<FStructProperty>(NULL);
		void* StructPtr = Stack.MostRecentPropertyAddress;

		P_FINISH;
		bool bSuccess = false;
		FStructProperty* StructProp = CastField<FStructProperty>(Stack.MostRecentProperty);
		if(StructProp && StructPtr)
		{
			UScriptStruct* OutputType = StructProp->Struct;
			if(OutputType)
			{
				P_NATIVE_BEGIN;
				bSuccess = Generic_JsonStringToStruct(JsonString,OutputType,StructPtr);
				P_NATIVE_END;
			}
		}
		*(bool*)RESULT_PARAM = bSuccess;
	}
	
	UFUNCTION(BlueprintCallable,CustomThunk,meta = (CustomStructureParam = "Struct"))
	static bool StructToJsonString(const int32& Struct, FString& OutputString);
	static bool Generic_StructToJsonString(FString& OutputString,UStruct* StructType, const void* StructPtr);
	DECLARE_FUNCTION(execStructToJsonString)
	{
		Stack.StepCompiledIn<FStructProperty>(NULL);
		void* StructPtr = Stack.MostRecentPropertyAddress;

		P_GET_PROPERTY_REF(FStrProperty, ToJsonString);
	
		P_FINISH;
		bool bSuccess = false;
		FStructProperty* StructProp = CastField<FStructProperty>(Stack.MostRecentProperty);
		if(StructProp && StructPtr)
		{
			UScriptStruct* OutputType = StructProp->Struct;
			if(OutputType)
			{
				P_NATIVE_BEGIN;
				bSuccess = Generic_StructToJsonString(ToJsonString,OutputType,StructPtr);
				P_NATIVE_END;
			}
		}
		*(bool*)RESULT_PARAM = bSuccess;
	}
	
	UFUNCTION(BlueprintPure,  Category = "CommonFuntionTesting")
	static FString UrlEncode(const FString UnencodedString);

	UFUNCTION(BlueprintPure,  Category = "CommonFuntionTesting")
	static FString ExtractSchemeAndAuthority(const FString& URL);

	UFUNCTION(BlueprintCallable,Category=Common)
	static void ExecuteCMDCommand(FString Parms);
        
	UFUNCTION(BlueprintCallable,Category=Common)
	static void OpenExe(FString URL);

	UFUNCTION(BlueprintCallable,Category=Common)
	UTexture2D* ConvertRenderTargetToTexture2D(UTextureRenderTarget2D* TextureRenderTarget);

	UFUNCTION(BlueprintCallable,Category=Common)
	static UTexture* LoadTexture2D(const FString& ImagePath,bool& IsValid,int32& OutWidth,int32& OutHeight);

	UFUNCTION(BlueprintCallable,Category=Common)
	static bool SaveTexture2D(UTexture2D* Texture, const FString& FilePath);

	UFUNCTION(BlueprintPure, Category="Common")
	static int ChineseNumberToArabicNumber(const FString& ChineseNumber);

	UFUNCTION(BlueprintPure, Category="Common")
	static FString ArabicNumberToChineseNumber(int ArabicNumber);

	UFUNCTION(BlueprintPure, Category="Common")
	static bool IsValidChineseNumber(const FString& ChineseNumber);

};
